// 使用Java Sound API播放音乐
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

class BasicPlayer implements Player {
    private Clip clip;
    private PlayerStatus status = PlayerStatus.STOPPED;
    private float volume = 0.5f;

    @Override
    public void play(AudioFile audioFile) {
        try {
            stop();
            // 使用Java Sound API获取音频输入流
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile.getFile());
            clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            
            // 设置音量
            FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
            float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
            gainControl.setValue(dB);
            
            clip.start();
            status = PlayerStatus.PLAYING;
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void pause() {
        if (clip != null && status == PlayerStatus.PLAYING) {
            clip.stop();
            status = PlayerStatus.PAUSED;
        }
    }

    @Override
    public void stop() {
        if (clip != null) {
            clip.stop();
            clip.close();
            status = PlayerStatus.STOPPED;
        }
    }

    @Override
    public void setVolume(float volume) {
        this.volume = Math.max(0.0f, Math.min(1.0f, volume));
        if (clip != null) {
            FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
            float dB = (float) (Math.log(this.volume) / Math.log(10.0) * 20.0);
            gainControl.setValue(dB);
        }
    }

    @Override
    public PlayerStatus getStatus() {
        return status;
    }
}